<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/7/1
 * Time: 10:16
 */
namespace Api\Controller;
use Think\Controller;
use Think\Model;

class InvoiceController extends BaseController
{
    //发票历史列表
    public function invoiceHistory(){
        $type=I('post.type','0','int');   //0待开票 1待邮寄 2已寄出
        $page=I('post.page','1','int');     //当前页
        $limit=I('post.limit',10,'int');    //每页显示记录数
        $user_id=I('post.user_id','','int');    //用户id

        if(empty($user_id)){
            $data=array(err_code=>2011,err_msg=>'必须有用户id');
            $this->ajaxReturn($data,'JSON');
        }

        if(!M('user')->where("user_id=$user_id")->find()){
            $data=array(err_code=>2011,err_msg=>'用户不存在');
            $this->ajaxReturn($data,'JSON');
        }

        //分页
        $where['status']=$type;
        $where['user_id']=$user_id;
        $count=M('invoice')->where($where)->count();
        $countPage=ceil($count/$limit);
        if($page>$countPage){
            $page=$countPage;
        }
        if($page<1){
            $page=1;
        }
        $offset=($page-1)*$limit;
        $list=M('invoice')->where($where)->field('number,name,add_time,money,address')->limit($offset,$limit)->select();
        $pages['countPage']=$countPage;
        $pages['now_page']=$page;
        $pages['exist_next']=$page>=$countPage?'false':'true';
        $data=array(err_code=>200,err_msg=>$list,page=>$pages);
        $this->ajaxReturn($data,'JSON');
    }


    //发票列表
    public function invoiceList(){
        $page=I('post.page','1','int');     //当前页
        $limit=I('post.limit',10,'int');    //每页显示记录数
        $user_id=I('post.user_id','','int');    //用户id

        if(empty($user_id)){
            $data=array(err_code=>2011,err_msg=>'必须有用户id');
            $this->ajaxReturn($data,'JSON');
        }
        $user=M('user')->where("user_id=$user_id")->find();

        if(empty($user)){
            $data=array(err_code=>2011,err_msg=>'用户不存在');
            $this->ajaxReturn($data,'JSON');
        }


        $where['status']=10;
        $where['user_id']=$user_id;
        $where['is_mail']=0;
        $count=M('invoice i')->where($where)->count();
        $countPage=ceil($count/$limit);
        if($page>$countPage){
            $page=$countPage;
        }
        if($page<1){
            $page=1;
        }
        $offset=($page-1)*$limit;
        $list=M('repair_order')->where($where)
            ->field('mode_charging,repair_order_num_alias,fault_id_list,total,fixed_price,set_time,amount_paid')
            ->limit($offset,$limit)->order('set_time desc')->select();



        $new_list=array();
        foreach($list as $k=>$v){
                 $date=date('Y-m',$v['set_time']);
                 //$list[$k]['money']=$v['fixed_price']?$v['fixed_price']:$v['total'];
                 $list[$k]['money']=$v['amount_paid'];
                 $list[$k]['mode_charging']=$v['mode_charging']==0?'顺风':$v['mode_charging']==1?'标准':'专修';
                 $list[$k]['add_time']=date('Y-m-d H:i:s',$v['set_time']);
            $whereo['fault_id']=array('in',$v['fault_id_list']);
            $name=M('fault')->where($whereo)->field('name')->select();
            $list[$k]['fault']='';
            foreach($name as $key=>$val){
                $list[$k]['fault'].=$val['name'].',';
            }
            $list[$k]['fault']=trim($list[$k]['fault'],',');
                 $new_list[$date][]=$list[$k];
        }
        $config=M('invoice_config')->where('status=1')->field('tax,mail_cost')->find();
        $data=array(err_code=>200,err_msg=>$new_list,config=>$config);
        $this->ajaxReturn($data,'JSON');

    }
}